Ext.define('Admin.view.auth.AuthenticationController', {
	        extend : 'Ext.app.ViewController',
	        alias : 'controller.authentication',

	        getReferrer : function() {
		        var ref = document.referrer;
		        return '/' == ref[ref.length - 1] ? ref.substring(0, ref.length - 1) : ref;
	        },

	        onQQLogin : function(button, e) {
		        window.location.href = this.getReferrer() + '/rest/oauth/sign/in/qq/start';
	        },

	        onWechatLogin : function(button, e) {
		        window.location.href = this.getReferrer() + '/rest/oauth/sign/in/wechat/start';
	        },

	        onGithubLogin : function(button, e) {
		        window.location.href = this.getReferrer() + '/rest/oauth/sign/in/github/start';
	        },

	        onUserLogin : function(button, e, eOpts) {
		        var me = this, form = me.getView();
		        Admin.Ajax.submit(form, {
			                method : 'GET',
			                url : 'rest/oauth/sign/in/basic',
			                fnSuccess : function(options, result) {
				                var app = Admin.getApplication(), view = app.getMainView(), ctrl = view.getController();
				                ctrl.redirectTo(app.getDefaultToken(), true);
			                }
		                });
	        },

	        onNewAccount : function(button, e, eOpts) {
		        this.redirectTo('userregister');
	        },

	        onSignupClick : function(button, e, eOpts) {
		        var me = this, form = me.getView(), refs = form.getReferences();

		        var record = Ext.create('Admin.model.account.User');
		        record.data.name = refs.login.getValue();
		        record.data.nicename = refs.nicename.getValue();
		        record.data.password = refs.password.getSubmitValue();

		        Admin.Ajax.submit(form, {
			                method : 'POST',
			                url : 'rest/oauth/sign/up',
			                mask : true,
			                fnSuccess : function(options, result) {
				                Admin.Msg.info(result.data, function() {
					                        me.redirectTo('login');
				                        });
			                }
		                }, record);
	        },

	        onResetNonceApplyClick : function(button, e, eOpts) {
		        var form = this.getView();
		        // TODO 先读取nonce
		        Admin.Ajax.submit(form, {
			                method : 'GET',
			                url : 'rest/oauth/pswd/reset',
			                mask : true,
			                fnSuccess : function(options, result) {
				                Admin.Msg.info(result.data);
			                }
		                });
	        },

	        onResetPasswordClick : function(button, e, eOpts) {
		        var me = this, form = me.getView();
		        Admin.Ajax.submit(form, {
			                method : 'GET',
			                url : 'rest/oauth/pswd/reset/complete',
			                mask : true,
			                fnSuccess : function(options, result) {
				                Admin.Msg.info(result.data, function() {
					                        me.redirectTo('login');
				                        });
			                }
		                });
	        },

	        onChangePasswordClick : function() {
		        var me = this, form = me.getView();
		        Admin.Ajax.submit(form, {
			                method : 'GET',
			                url : 'rest/oauth/pswd/change',
			                fnSuccess : function(options, result) {
				                Admin.Msg.info(result.data, function() {
					                        window.location.replace(window.location.origin);
				                        });
			                }
		                });
	        }
        });
